package net.sangpil.classes.net
{
	import flash.events.EventDispatcher;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	import net.sangpil.classes.events.Amf_PHP_Event;
	
	[Event(name = "when_data_back",type = "net.sangpil.classes.events.Amf_PHP_Event")]
	public class Amf_PHP extends EventDispatcher
	{
		private var _gatewayPath:String;
		private var _serviceName:String;
		private var _nc:NetConnection;
		private var _res:Responder;
		
		public function Amf_PHP(gatewayPath:String) 
		{
			super();
			_gatewayPath = gatewayPath;
			_nc = new NetConnection();
			_nc.objectEncoding = ObjectEncoding.AMF0;
			_res = new Responder(onResult, onFault);
		}
		
		public function getSqlData(serviceName:String, ...args:*):void
		{
			trace("------------------service name : ", serviceName);
			_serviceName = serviceName;
			_nc.connect(_gatewayPath);
			if(args.length > 0)
			{
				var reargs:Array = new Array;
				reargs.push(_serviceName);
				reargs.push(_res);
				
				for(var i:uint = 0; i < args.length; i++)
				{
					reargs.push(args[i]); 
				} 
				var callFunction:Function = _nc.call;
				callFunction.apply(_nc, reargs);
			}else{
				_nc.call(_serviceName, _res);
			}
			
		}
		/**
		 *  
		 * @param serviceName
		 * @param args
		 * 
		 */		
		public function insertSqlData(serviceName:String, ...args:*):void
		{
			trace("------------------service name : ", serviceName);
			_serviceName = serviceName;
			_nc.connect(_gatewayPath);
			
			var reargs:Array = new Array;
			reargs.push(_serviceName);
			reargs.push(_res);
			
			for(var i:uint = 0; i < args.length; i++)
			{
				reargs.push(args[i]); 
			} 
			var callFunction:Function = _nc.call;
			callFunction.apply(_nc, reargs);
		}
		
		public function editSqlData(serviceName:String, ...args):void
		{
			trace("------------------service name : ", serviceName);
			_serviceName = serviceName;
			_nc.connect(_gatewayPath);
			
			var reargs:Array = new Array;
			reargs.push(_serviceName);
			reargs.push(_res);
			
			for(var i:uint = 0; i < args.length; i++)
			{
				reargs.push(args[i]); 
			} 
			var callFunction:Function = _nc.call;
			callFunction.apply(_nc, reargs);
		}
		
		public function deleteSqlData(serviceName:String, ...args):void
		{
			trace("------------------service name : ", serviceName);
			_serviceName = serviceName;
			_nc.connect(_gatewayPath);
			
			var reargs:Array = new Array;
			reargs.push(_serviceName);
			reargs.push(_res);
			
			for(var i:uint = 0; i < args.length; i++)
			{
				reargs.push(args[i]); 
			} 
			var callFunction:Function = _nc.call;
			callFunction.apply(_nc, reargs);
		}
		
		private function onResult(responds:Object):void
		{
			trace("connect success");
			this.dispatchEvent(new Amf_PHP_Event(responds, Amf_PHP_Event.WHEN_DATA_BACK));
		}
		
		private function onFault(responds:Object):void
		{
			trace("connect fault");
			for(var i:String in responds)
			{
				trace(i, " = ", responds[i]);
			}
		}
		
		public function get serviceName():String
		{
			return _serviceName;
		}
		
		public function set serviceName(serviceName:String):void
		{
			_serviceName = serviceName;
		}
	}
}